'use strict';


/**
 * 检测设备
 */
if(!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
    console.log("enumerateDevices() not supported.");
}
navigator.mediaDevices.enumerateDevices().then((deviceInfos) => {
    deviceInfos.forEach(deviceInfo => {
      // console.log(deviceInfo.kind + ": " + deviceInfo.label + " id = " + deviceInfo.deviceId);
    })
}).catch(e => {
    console.log(err.name + ": " + err.message);
})

/**
 * 获取视频流并展示
 */
const mediaStreamContrains = {
  video: {
      frameRate: {min: 20},
      width: {min: 640, ideal: 1280},
      height: {min: 360, ideal: 720},
      aspectRatio: 16/9
  },
  audio: {
      echoCancellation: true,
      noiseSuppression: true,
      autoGainControl: true
  }
};

const video = document.querySelector('video');

navigator.mediaDevices.getUserMedia(mediaStreamContrains).then(res => {
    video.srcObject = res;
}).catch(e => {
    console.log('navigator.getUserMedia error: ', e);
})

/**
 * 拍照
 */
const canvas = document.querySelector('#picture');
canvas.width = 640
canvas.height = 480

function takePic() {
    const pic = canvas.getContext('2d');
    pic.drawImage(video, 0,0, canvas.width, canvas.height)
}

/**
 * 设置滤镜等
 */
function setPicFilter(num) {
    
}

/**
 * 下载照片
 */
function downLoad() {
    let oA = document.createElement('a');
    oA.download = 'phone'; // 下载名
    oA.href = canvas.toDataURL("image/jpeg");
    document.body.appendChild(oA)
    oA.click()
    oA.remove()
}
